package com.yiren.manager.ctrl.elder;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.yiren.core.exception.MyException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderArrearage;
import com.yiren.entity.YrElderBeHospital;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrPaper;
import com.yiren.entity.YrPaperRecord;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.service.iface.elder.YrElderBeHospitalService;

public abstract class YrElderBaseController<F, T> extends GenericController<F, T> {

	@Autowired
	YrElderBeHospitalService elderBeHospitalService;

	/**
	 * 构造一个老者欠费记录
	 * 
	 * @param elder
	 * @param arrearageType
	 * @param relationId
	 * @param money
	 * @param reason
	 * @return
	 * @throws MyException
	 */
	public YrElderArrearage constElderArreagrage(String beHospitalId, YrElder elder, Integer arrearageType,
			String relationId, Float money, String reason, String decs, String createId) throws MyException {
		YrElderArrearage elderArrearage = new YrElderArrearage();
		elderArrearage.setElderArrearageId(UUIDGenerator.getUUID());
		String sqlWhere = "elder_id=?1 AND be_status=0";
		QueryResult<YrElderBeHospital> qrElderBeHospital = elderBeHospitalService.find(YrElderBeHospital.class,
				sqlWhere, new Object[] { elder.getElderId() });
		if (qrElderBeHospital.getTotalRecord() > 0) {
			elderArrearage.setSubjectRoomId(qrElderBeHospital.getResultList().get(0).getSubjectRoomId());
		}
		elderArrearage.setRelationId(relationId);
		elderArrearage.setBeHospitalId(beHospitalId);
		elderArrearage.setArrearageType(arrearageType);
		elderArrearage.setElderId(elder.getElderId());
		elderArrearage.setElderName(elder.getName());
		elderArrearage.setElderCardId(elder.getCardId());
		elderArrearage.setArrearageMoney(money);
		elderArrearage.setArrearageReason(reason);
		elderArrearage.setShouldPayDate(new Date());
		elderArrearage.setCreateTime(new Date());
		elderArrearage.setCreateId(createId);
		elderArrearage.setStatus(0);
		elderArrearage.setArrearageDesc(decs);

		return elderArrearage;
	}

	/**
	 * 
	 * @param behospitalId
	 * @param decs
	 * @param elder
	 * @param costType
	 *            费用类型(1基础服务费2护理费3个性服务费4床位费5水费6电费7热水费8院内租赁9代购用品10饭卡充值11rfid卡12欠费补交13手工扣费)
	 * @param relationId
	 * @param money
	 * @param payWay
	 *            支付方式(1现金2钱包3vip卡)
	 * @param payType
	 *            缴费类型(0正常缴费1补交2退费)
	 * @param createId
	 * @return
	 */
	public YrElderPayRecord costElderPayRecord(String behospitalId, String decs, YrElder elder, Integer costType,
			String relationId, Float money, Integer payWay, Integer payType, String createId, Integer quantity) {
		YrElderPayRecord elderPayRecord = new YrElderPayRecord();
		elderPayRecord.setElderPayRecordId(UUIDGenerator.getUUID());
		elderPayRecord.setCostDesc(decs);
		elderPayRecord.setPayDate(new Date());
		elderPayRecord.setElderId(elder.getElderId());
		elderPayRecord.setElderCardId(elder.getCardId());
		elderPayRecord.setElderName(elder.getName());
		elderPayRecord.setBeHospitalId(behospitalId);
		elderPayRecord.setPayStatus(1);
		elderPayRecord.setPayWay(payWay);
		elderPayRecord.setCostType(costType);
		elderPayRecord.setCreateTime(new Date());
		elderPayRecord.setRelationId(relationId);
		elderPayRecord.setPayMoney(money);
		elderPayRecord.setPayType(payType);
		elderPayRecord.setCreateId(createId);
		elderPayRecord.setQuantity(quantity);
		return elderPayRecord;
	}

	/**
	 * 创造一条打印记录
	 * @param paper
	 * @param beHospitalId
	 * @param staffId
	 * @param money
	 * @param paperType
	 * @return
	 */
	public YrPaperRecord costPaperRecord(YrPaper paper,String beHospitalId,
			String staffId,Float money,Integer paperType) {
		// 增加一条打印记录
		YrPaperRecord paperRecord = new YrPaperRecord();
		paperRecord.setBeHospitalId(beHospitalId);

		paperRecord.setStaffId(staffId);
		paperRecord.setPaperType(paperType);
		paperRecord.setPaperNum(paper.getUseNum());
		paperRecord.setStartNum(paper.getStartNum());
		paperRecord.setEndNum(paper.getEndNum());
		paperRecord.setAmount(money);
		paperRecord.setCreateTime(new Date());
		paperRecord.setPaperRecordId(UUIDGenerator.getUUID());
		paperRecord.setStatus(0);
		paperRecord.setCreateId(staffId);
		paperRecord.setPrintDate(new Date());
		paperRecord.setBatchNum(paper.getBatchNum());
		
		return paperRecord;
	}
	
	/**
	 * 得到下一个发票号码
	 * @param pagerNum
	 * @return
	 */
	public static String toNumberStr(String pagerNum){
		int length = pagerNum.length();
		int num = Integer.parseInt(pagerNum)+1;
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<length;i++){
			if(pagerNum.charAt(i)=='0'){
				sb.append('0');
			}else{
				break;
			}
		}
		return sb.append(num).toString();
	}
}
